package com.jsbb.controller.sys;

import com.jsbb.core.Message;
import com.jsbb.core.PageVo;
import com.jsbb.dto.sys.*;
import com.jsbb.service.sys.SysUserService;
import com.jsbb.util.ExcelUtils;
import com.jsbb.vo.sys.SysUserDetailVo;
import com.jsbb.vo.sys.SysUserListVo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * 用户管理Controller
 *
 * @author jsbb
 */
@RestController
@RequestMapping("/sys/user")
@Tag(name = "1.1 系统管理-用户管理")
public class SysUserController {

    @Autowired
    private SysUserService sysUserService;

    @PostMapping("/list")
    @Operation(summary = "获取列表")
    public Message<PageVo<SysUserListVo>> list(@RequestBody SysUserQueryDto queryDto) {
        PageVo<SysUserListVo> pageVo = sysUserService.list(queryDto);
        return Message.success(pageVo);
    }

    @GetMapping("/get/{id}")
    @Operation(summary = "获取详情")
    @Parameter(name = "id", description = "ID")
    public Message<SysUserDetailVo> get(@PathVariable("id") Long id) {
        SysUserDetailVo userVo = sysUserService.get(id);
        return Message.success(userVo);
    }

    @PostMapping("/add")
    @Operation(summary = "新增")
    public Message add(@Validated @RequestBody SysUserAddDto addDto) {
        sysUserService.add(addDto);
        return Message.success();
    }

    @PostMapping("/update")
    @Operation(summary = "修改")
    public Message update(@Validated @RequestBody SysUserUpdateDto updateDto) {
        sysUserService.update(updateDto);
        return Message.success();
    }

    @PostMapping("/delete/{id}")
    @Operation(summary = "删除")
    @Parameter(name = "id", description = "ID")
    public Message delete(@PathVariable("id") Long id) {
        sysUserService.delete(id);
        return Message.success();
    }

    @PostMapping("imports")
    @Operation(summary = "导入")
    @Parameter(name = "file", description = "文件")
    public Message imports(MultipartFile file) throws Exception {
        String fileName = file.getOriginalFilename();
        if (fileName == null || "".equals(fileName)) {
            return null;
        }
        String[] columns = new String[]{"userName", "fullName", "sex", "birthDate", "phone"};
        List<SysUserAddDto> list = ExcelUtils.importExcel(file, SysUserAddDto.class, columns);
        for (SysUserAddDto addDto : list) {
            sysUserService.add(addDto);
        }
        return Message.success();
    }

    @PostMapping(value = "export")
    @Operation(summary = "导出")
    public void export(@RequestBody SysUserQueryDto queryDto, HttpServletRequest request, HttpServletResponse response) {
        queryDto.setPageSize(10000000);
        PageVo pageVo = sysUserService.list(queryDto);
        List<SysUserListVo> list = pageVo.getList();
        String[] headers = new String[]{"用户名", "姓名", "性别", "出生日期", "手机号码", "创建时间"};
        String[] columns = new String[]{"userName", "fullName", "sex", "birthDate", "phone", "createTime"};
        ExcelUtils.exportExcel(list, headers, columns, request, response, "用户");
    }
}
